<?php
namespace com\mvc\orm\dao;
class MysqliDB implements DBInterfaz{
	private static $instance;
	private $db;
	private $error;
	
	private $statement;
	private $where;
	private $bind_params;
	private $groupBy;
	private $columns;
	private $tables;
	private static $iterator_names = 0;
	private $forcedQuery;
	
	public function __construct(){
		$this->db = new \mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
		if($this->db->connect_error) {
			$this->error = 'Imposible conectar a la BBDD. ' . $this->db->connect_error;
		}
	}
	
	public static function singleton(){
		if (!self::$instance instanceof self)
		{
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	public function getDB(){
		return $this->db;
	}
	
	public function __clone(){
		trigger_error("Clonación no permitida",E_USER_ERROR);
	}
	
	public function forcedQuery($query){
		$this->forcedQuery = $query;
	}
	
	public function addBindingParams($binding){
		if (!isset($this->bind_params)){
			$this->bind_params = array();
		}
		
		if (is_object($binding) && $binding instanceof \com\mvc\orm\dao\GenericObjectDB){
			if (count($this->bind_params)==0){
				$this->bind_params[] = $this->mappingTypes($binding->getType());
				$this->bind_params[] = $binding->getValue();
			}else{
				$this->bind_params[0] .= $this->mappingTypes($binding->getType());
				$this->bind_params[] = $binding->getValue();
			}
		}else if (is_array($binding)){
			foreach ($binding as $value){
				if (count($this->bind_params)==0){
					$this->bind_params[] = $this->mappingTypes($value->getType());
					$this->bind_params[] = $value->getValue();
				}else{
					$this->bind_params[0] .= $this->mappingTypes($value->getType());
					$this->bind_params[] = $value->getValue();
				}
			}
		}
	}
	
	public function addWhere(){
	}
	public function addColumn(){
		$args = func_get_args();
		if (isset($this->columns) && $this->columns!=null){
			$this->columns = array();
		}
		foreach ($args as $value){
			$this->columns[] = $value;
		}
		return "addColumn";
	}
	public function addTables(){
		return "addTables";
	}
	public function addGroupBy(){
	
	}
	
	
	public function mappingTypes($type){
		switch ($type) {
			case "integer":
				$result = "i";
				break;
			case "string":
				$result = "s";
				break;
			case "double":
				$result = "d";
				break;
			case "blob":
				$result = "b";
				break;
			default:
				$result = "s";
				break;
		}
		return $result;
	}
	
	public function query(){
		$result = array();
		if ($this->forcedQuery){
			$this->statement = $this->db->prepare($this->forcedQuery);
			if($this->statement === false) {
				trigger_error('SQL Incorrecto: ' . $this->forcedQuery . ' Error: ' . $this->db->errno . ' ' . $this->db->error, E_USER_ERROR);
			}
			$queryParams = array();
			foreach ($this->bind_params as $key=>$value){
				if ($key==0){
					$queryParams[] = $this->bind_params[$key];
				}else{
					$queryParams[] = &$this->bind_params[$key];
				}
			}
			call_user_func_array(array($this->statement,'bind_param'),$queryParams);
			
			$this->statement->execute();
			$queryParams = array();
			foreach ($this->columns as $key=>$value){
				$queryParams[] = &$this->columns[$key];
			}
		 	//$res = $this->statement->get_result();
			call_user_func_array(array($this->statement,'bind_result'),$queryParams);
			
			while($this->statement->fetch()){
				$row = array();
				foreach ($this->columns as $value){
					$row[] = $value;
				}
				$result[] = $row;
			}
			
		}else{
			
		}
		
		return $result;
	}
	public function insert(){
		return "insert";
	}
	public function update(){
		return "update";
	}
	public function delete(){
		return "delete";
	}
	
	public function reset(){
		unset($this->bind_params);
		unset($this->statement);
		unset($this->bind_params);
		unset($where);
		unset($groupBy);
		unset($columns);
		unset($tables);
	}
	
	public function closeConnection(){
		if (isset($this->statement)){
			$this->statement->close();
			unset($this->statement);
		}
		if (isset($this->db)){
			$this->db->close();
			unset($this->db);
		}
		unset($this->bind_params);
	}
	public function closeStatement(){
		if (isset($this->statement)){
			$this->statement->close();
			unset($this->statement);
		}
		unset($this->bind_params);
	}
	
	private function fillWhere(&$value){
		if ($value){
			if ($value->getNames()){
				if (!$this->bind_params){
					$this->bind_params = array();
				}
				$bind_param = array();
				if ($value->getType()){
					$bind_param["type"] = $value->getType(); 
				}
				if ($value->getName()){
					$bind_param["name"] = $value->getName();
				}
			}
			if ($value->getValue()){
				if ($this->where){
					$this->where = "";
				}
				$this->tables = $value->getColumn();
			}
		}
	}
}